package com.ycx.furns.entity;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Cart {
    public final Map<Integer, Item> map = new HashMap<>();

    public boolean isEmpty(){
        return map.isEmpty();
    }

    public Collection<Item> getItems(){
        return (Collection<Item>) map.values();
    }

    public void update(Integer id, int num){
        Item item = map.get(id);
        if(item != null){

            item.setNum(num);
            item.setTotalPrice(item.price.multiply(new BigDecimal(item.getNum())));
        }
    }

    public BigDecimal getTotalPrice(){
        BigDecimal totalPrice = new BigDecimal(0);
        Collection<Item> items = getItems();
        for (Item item : items) {
            totalPrice = totalPrice.add(item.getTotalPrice());
        }
        return  totalPrice;
    }

    public int getTotalCount(){
        int itemCount = 0;
        Collection<Item> values = map.values();
        for (Item item : values) {
            itemCount += item.getNum();
        }
        return itemCount;
    }

    public void clear(){
        map.clear();
    }
    public void addItem(Item item){
        Integer id = item.getId();
        String name = item.getName();
        BigDecimal price = item.getPrice();
        String imgPath = item.getImg_path();
        Item item1 = map.get(id);
        if(item1 != null){
            item1.setNum(item1.getNum() + 1);
            item1.setTotalPrice(item1.getTotalPrice().add(item1.getPrice()));
            return;
        }
        map.put(id, new Item(id, name, price, 1, price, item.getImg_path()));
    }

    public void deleteItem(Integer id){
        map.remove(id);
    }

    public void minusItem(Integer id){
        if(map.get(id).getNum() == 1){
            deleteItem(id);
            return;
        }

        Item item = map.get(id);
        item.setNum(item.getNum() - 1);
        item.setTotalPrice(item.getTotalPrice().subtract(item.getPrice()));
    }

    public static class Item{
        Integer id;
        String name;
        BigDecimal price;
        int num;
        BigDecimal totalPrice;
        String img_path = "assets/images/product-image/default.jpg";

        public Item() {
        }

        public Item(int id, String name, BigDecimal price, int num, BigDecimal totalPrice, String img_path) {
            this.id = id;
            this.name = name;
            this.price = price;
            this.num = num;
            this.totalPrice = totalPrice;
            if(!(img_path == null || "".equals(img_path))){
                this.img_path = img_path;
            }
        }

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public BigDecimal getPrice() {
            return price;
        }

        public void setPrice(BigDecimal price) {
            this.price = price;
        }

        public int getNum() {
            return num;
        }

        public void setNum(int num) {
            this.num = num;
        }

        public BigDecimal getTotalPrice() {
            return totalPrice;
        }

        public void setTotalPrice(BigDecimal totalPrice) {
            this.totalPrice = totalPrice;
        }

        public String getImg_path() {
            return img_path;
        }

        public void setImg_path(String img_path) {
            if(!(img_path == null || "".equals(img_path))){
                this.img_path = img_path;
            }
        }

        @Override
        public String toString() {
            return "Item{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", price=" + price +
                    ", num=" + num +
                    ", totalPrice=" + totalPrice +
                    ", img_path='" + img_path + '\'' +
                    '}';
        }
    }
}
